package keter.web.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import keter.domain.Unit;
import keter.service.CodeListService;
import keter.service.UnitService;
import keter.web.util.MediaTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
//@RequestMapping("/console/unit")
public class UnitController extends KeterBaseController{
	
	@Autowired
	CodeListService service;  
	
	@Autowired
	UnitService us;  

	@RequestMapping(value = "/public/unitof/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	@Deprecated
	public List<Unit> getPublicUnitByParentId(@PathVariable Long id) throws Exception {
		return us.getDao().findByParentId(id);
	}
	
	/**
	 * <p>Method ：getAllUnitByParentId
	 * <p>Description : 接收post请求的方式获取组织机构信息 获取全部组织机构节点，适合学生注册、修改个人信息场景
	 * @param request
	 * @return
	 * @throws Exception 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "/public/unitof/", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public List<Unit> getPublicUnitByParentId(HttpServletRequest request) throws Exception {
		Long id = Long.valueOf((String) request.getParameter("id"));
		List<Unit> list = us.getDao().findByParentId(id);
		return list;
	}
	
	
	@RequestMapping(value = "/unit/unitof/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	@Deprecated
	public List<Unit> getUnitByParentId(@PathVariable Long id) throws Exception {
		return us.getDao().findByParentId(id);
	}
	
	/**
	 * <p>Method ：getUnitByParentId
	 * <p>Description : 根据角色获取组织机构，适合教师、管理员修改个人信息场景
	 *    请求路径必须添加“/console”，否则无法获取“getCurrentAccount”，因为安全机制已将"/util/xxx"视为匿名访问，导致系统认为当前用户并未登录
	 * @param request
	 * @return
	 * @throws Exception 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "/unit/unitof/", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public List<Unit> getUnitByParentId(HttpServletRequest request) throws Exception {
		Long id = Long.valueOf((String) request.getParameter("id"));
		List<Unit> list = us.getDao().findByParentId(id);
		//强制教师只能选择“专业”
		if(getCurrentAccount()!=null && getCurrentAccount().hasRole("TEACHER") && !getCurrentAccount().hasRole("ADMIN")){
			for (Iterator<Unit> iterator = list.iterator(); iterator.hasNext();) {
				Unit unit = (Unit) iterator.next();
				if(unit.getCode().length()==6){//通过组织机构代码长度判断当前unit为"专业"
					unit.setIsParent(false);
				}
			}
		}
		return list;
	}
	
	@RequestMapping(value = "/console/unit", method = RequestMethod.GET)
	public String index(ModelMap model) {
		return "console/unit/unit_index";
	}
	
	@RequestMapping(value = "/console/unit/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		return "console/unit/unit_index";
	}
	
	/**
	 * <p>Method ：update
	 * <p>Description : 更新数据
	 * @param topic
	 * @param response
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Update, method = RequestMethod.POST, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> update(@RequestBody Unit u) {
    	Map<String, String> ajaxMap = getAjaxMap();
    	us.getDao().save(u);
		return ajaxMap;
	}
	
}
